package com.lancoo.answer.view.xgPaResultView.adapter

import android.content.Context
import android.text.Html
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvXgAdapterAnswersheetParentBinding
import com.lancoo.answer.model.bean.CognitiveBean
import com.lancoo.answer.model.bean.XgPaResultBean
import com.lancoo.answer.util.DensityUtils
import com.lancoo.answer.util.DoubleUtils
import com.lancoo.answer.util.MethodUtils
import com.lancoo.answer.util.SpacesItemDecoration
import com.lancoo.answer.view.base.XBaseRecyclerAdapter
import com.lancoo.answer.view.base.XBaseRecyclerHolder
import com.lancoo.answer.view.xgPaResultView.adapter.XgPaResultAnswerSheetChildAdapter.OnClickChildListener

class XgPaResultAnswerSheetParentAdapter(
    val context: Context,
    val cognitiveBean: CognitiveBean
) : XBaseRecyclerAdapter<EvXgAdapterAnswersheetParentBinding>() {

    override fun onBindingView(viewGroup: ViewGroup?): EvXgAdapterAnswersheetParentBinding {
        return EvXgAdapterAnswersheetParentBinding.inflate(
            LayoutInflater.from(
                viewGroup!!.context
            ), viewGroup, false
        )
    }

    override fun onBindingData(
        holder: XBaseRecyclerHolder<EvXgAdapterAnswersheetParentBinding>,
        position: Int
    ) {
        val typeIndex = cognitiveBean.TypeList!![position].TypeIndex
        val strTypeIndex = MethodUtils.numberToChinese(typeIndex)
        var genreName = cognitiveBean.TypeList!![position].GenreName
        val typeName = cognitiveBean.TypeList!![position].TypeName
        if (genreName!!.isEmpty()) {
            genreName = typeName
        }
        var evalScore = cognitiveBean.TypeList!![position].EvalScore
        var totalScore = cognitiveBean.TypeList!![position].TotalScore
        totalScore = DoubleUtils.subZeroAndDot(totalScore!!)
        //显示分数及准确率颜色
        if (evalScore == "--") {
            holder.viewBinding.TvTypeInfor.text =
                Html.fromHtml(
                    context.resources.getString(
                        R.string.ev_result_ques_title_blue,
                        strTypeIndex, genreName, evalScore, totalScore
                    )
                )
        } else {
            evalScore = DoubleUtils.subZeroAndDot(evalScore!!)
            val correctRate =
                MethodUtils.getBusiness(evalScore!!.toDouble(), totalScore!!.toDouble())
            if (correctRate >= 0.6) {
                holder.viewBinding.TvTypeInfor.text =
                    Html.fromHtml(
                        context.resources.getString(
                            R.string.ev_result_ques_title_blue,
                            strTypeIndex, genreName, evalScore, totalScore
                        )
                    )
            } else {
                holder.viewBinding.TvTypeInfor.text =
                    Html.fromHtml(
                        context.resources.getString(
                            R.string.ev_result_ques_title_blue,
                            strTypeIndex, genreName, evalScore, totalScore
                        )
                    )
            }
        }

        /*******************************************************************************************/
        val sortIndex = cognitiveBean.TypeList!![position].QuesList!![0].SortIndexList!![0].NewSortIndex
        if (sortIndex!!.contains("-")) {
            val space = DensityUtils.dp2px(context,0f)
            holder.viewBinding.RvAnswerSheetChild.addItemDecoration(SpacesItemDecoration(space))
            holder.viewBinding.RvAnswerSheetChild.layoutManager = GridLayoutManager(context, 1)
        } else {
            val space = DensityUtils.dp2px(context,0f)
            holder.viewBinding.RvAnswerSheetChild.addItemDecoration(SpacesItemDecoration(space))
            if(DensityUtils.isPad(context)){
                holder.viewBinding.RvAnswerSheetChild.layoutManager = GridLayoutManager(context, 10)
            }else {
                holder.viewBinding.RvAnswerSheetChild.layoutManager = GridLayoutManager(context, 7)
            }
        }
        /*******************************************************************************************/

        val params: LinearLayout.LayoutParams = holder.viewBinding.RvAnswerSheetChild.layoutParams as LinearLayout.LayoutParams
        params.setMargins(DensityUtils.dp2px(context,0f), 0, DensityUtils.dp2px(context,0f), 0)
        holder.viewBinding.RvAnswerSheetChild.layoutParams = params
        val adapterChild = XgPaResultAnswerSheetChildAdapter(
            context, cognitiveBean, position
        )
        holder.viewBinding.RvAnswerSheetChild.adapter = adapterChild

        adapterChild.setClickChildListener(object : OnClickChildListener {
            override fun onClick(
                index_A: Int,
                index_B: Int,
                index_C: Int
            ) {
                clickParentListener?.onClick(index_A, index_B, index_C)
            }
        })
    }

    override fun getItemCount(): Int {
        return cognitiveBean.TypeList!!.size
    }

    interface OnClickParentListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickParentListener: OnClickParentListener? = null

    fun setClickParentListener(clickParentListener: OnClickParentListener?) {
        this.clickParentListener = clickParentListener
    }

}